package y23.Sep;

import java.util.Arrays;

public class Test12 {
    public static void main(String[] args) {
        Solution5 solution5 = new Solution5();
        int[] arr = {2,7,4,1,8,1};
        System.out.println(solution5.lastStoneWeight(arr));
    }
}

class Solution5 {
    public int lastStoneWeight(int[] stones) {
        int s = stones.length;
        while (s > 1) {
            int sl = stones.length;
            Arrays.sort(stones);
            if (stones[sl - 1] == stones[sl - 2]) {
                stones[sl - 1] = 0;
                stones[sl - 2] = 0;
            } else {
                stones[sl - 1] -= stones[sl - 2];
                stones[sl - 2] = 0;
            }
            s--;
        }
        return stones[stones.length - 1];
    }
}
